;;; drupal6.el --- Skeletons for drupal-6

;; Copyright (C) 2010  Дмитрий Бухтияров

;; Author: Дмитрий Бухтияров <buhtiyarov@effy.eu>

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; Skeletons for drupal-6 modules & themes development

;;; Code:

(require 'skeleton)
(require 'php-mode)

(defun drupal6-module-name ()
  "Get the module name"
  (file-name-nondirectory (car (split-string (buffer-file-name) "\\."))))

(define-skeleton drupal6-theme-info
  "Theme info tempalte"
  "Theme name: "
  "name = " str \n
  "description = " (skeleton-read "description: ") & \n | -14
  "core = 6.x" \n
  "engine = phptemplate" \n
  "version = " (skeleton-read "version: " "6.x-dev") \n
  "base theme = " (skeleton-read "base theme: " "zen") & \n | -13
  \n
  "stylesheets[all][] = style.css" \n
  \n
  "scripts[] = script.js" \n
  \n
  ("region key: " "regions[" str "] = " (skeleton-read "region title: ") \n)
  \n
  ("feature: " "features[] = " str \n)
  \n)

(define-skeleton drupal6-module-info
  "Info template"
  "Module name: "
  "name = " str \n
  "description = " (skeleton-read "description: ") \n
  "version = " (skeleton-read "version: " "6.x-dev") \n
  "core = 6.x" \n
  "package = Effy" \n)

(defun drupal6-info ()
  "Theme or module info"
  (if (string-match "themes" (buffer-file-name)) (drupal6-theme-info) (drupal6-module-info)))

(define-skeleton drupal6-hook-menu
  "Insert hook_menu()"
  ""
  "/**\n"
  " * Implementation of hook_menu().\n"
  " */\n"
  "function " (drupal6-module-name) "_menu() {" \n
  "$items = array();" \n
  \n
  ("Menu path: "
   "$item['" str "'] = array(" \n
   "'title' => '" (skeleton-read "title: ") "'," \n
   "'description' => '" (skeleton-read "description: ") & "'," & \n | -18
   "'page callback' => '" (skeleton-read "page callback: ") "'," \n
   "'page arguments' => array(" (skeleton-read "page arguments: ") & ")," & \n | -26
   "'access callback' => '" (skeleton-read "access callback: ") & "'," & \n | -22
   "'access arguments' => array(" (skeleton-read "access arguments: " "'access content'") ")," \n
   "'file' => '" (skeleton-read "file: ") & "'," & \n | -11
   "'file path' => '" (skeleton-read "file path: ") & "'," & \n | -16
   "'type' => " (skeleton-read "type: ") & "," & \n | -10
   -2 ");" \n
   )
  resume:
  _ \n
  "return $items;" \n
  -2 "}" \n)

(define-skeleton drupal6-hook-block
  "Insert hook_block()"
  nil
  "/**\n"
  " * Implementation of hook_block().\n"
  " */\n"
  "function " (drupal6-module-name) "_block($op = 'list', $delta = 0, $edit = array()) {" \n
  "if ($op == 'list') {" \n
  "$blocks = array();" \n
  \n
  '(setq i 0)
  ("block info: "
   "$blocks[" (int-to-string i) "] = array(" \n
   "'info' => '" str "'," \n
   "'weight' => " (skeleton-read "weight: " "0") ","\n
   "'status' => " (skeleton-read "status: " "1") "," \n
   "'region' => '" (skeleton-read "region: ") & "'," & \n | -13
   "'cache' => " (skeleton-read "cache (BLOCK_CACHE_PER_{PAGE,USER,ROLE}, BLOCK_CACHE_GLOBAL): ") & "," & \n | -11
   "'visibility' => " (skeleton-read "visibility: ") & "," & \n | -16
   "'pages' => '" (skeleton-read "pages: ") & "'," & \n | -12
   -2 ");" \n
   \n
   '(setq i (1+ i)))
   "return $blocks;" \n
  -2 "} elseif ($op == 'view') {" \n
  "switch($delta) {" \n
  '(setq i 0)
  ("block function: "
   "case " (int-to-string i) ":" \n
   "return array(" \n
   "'content' => " str "()," \n
   "'subject' => '" (skeleton-read "block subject: ") & "'," & \n | -14
   ");" \n
   '(setq i (1+ i)))
  -2 "}" \n
  "return $block;" \n
  -2 "}" \n
  resume:
  -2 "}" \n)

;; hook_nodeapi
(define-skeleton drupal6-hook-nodeapi
  "Insert hook_nodeapi()"
  "\n")

;; hook_form_alter
(define-skeleton drupal6-hook-form-alter
  "Insert hook_form_alter()"
  nil
  "/**\n"
  " * Implementation of hook_form_alter().\n"
  " */" \n
  "function " (drupal6-module-name) "_form_alter(&$form, &$form_state, $form_id) {" \n
  > _ \n
  -2 "}" \n)

;; form item
(define-skeleton drupal6-form-item
  "Insert form item"
  "Form key: "
  "$form['" str "'] = array(" \n
  "'#type' => '" (skeleton-read "type: ") "'," \n
  "'#title' => '" (skeleton-read "title: ") "'," \n
  "'#description' => '" (skeleton-read "description: ") & "'," & \n | -19
  "'#default_value' => $form_state['values']['" str "']," \n
  "'#required' => " (skeleton-read "required: ") & "," & \n | -15
  "'#size' => " (skeleton-read "size: ") & "," & \n | -11
  resume:
  -2 ");" \n)

;; form
(define-skeleton drupal6-form
  "Insert form"
  "Form name: "
  "function " (drupal6-module-name) "_" str "_form($form_state) {" \n
  "$form = array();" \n
  \n
  _ \n
  \n
  "return $form;" \n
  -2 "}" \n
  \n
  "function " (drupal6-module-name) "_" str "_form_validate($form, &$form_state) {" \n
  \n
  -2 "}" \n
  \n
  "function " (drupal6-module-name) "_" str "_form_submit($form, &$form_state) {" \n
  \n
  -2 "}" \n)

(define-skeleton drupal6-install-schema
  "Insert install schema"
  nil
  "function " (drupal6-module-name) "_schema() {" \n
  "$schema = array();"\n
  \n
  _ \n
  \n
  "return $schema;" \n
  -2 "}" \n)

(define-skeleton drupal6-install-schema-table
  "Insert install schema table"
  "Table name: "
  "$schema['" (drupal6-module-name) "_" str "'] = array(" \n
  "'description' => '" (skeleton-read "description: ") & "'," & \n | -18
  "'fields' => array(" \n
  (drupal6-install-schema-table-field)
  -2 ")," \n
  "'primary key' => array(" (skeleton-read "primary key: ") ")," \n
  -2 ");" \n)

(define-skeleton drupal6-install-schema-table-field
  "Insert install schema table field"
  "Field name: "
  "'" str "' => array(" \n
  "'description' => '" (skeleton-read "description: ") & "'," & \n | -18
  "'type' => '" (setq type (skeleton-read "type: ")) "'," \n
  (when (string-equal type "varchar") (concat "'length' => " (skeleton-read "length: ") "," )) & \n
  (when (string-equal type "int") (concat "'unsigned' => " (skeleton-read "unsigned: " "TRUE") "," )) & \n
  "'not null' => " (skeleton-read "not null: " "TRUE") "," \n
  "'default' => " (skeleton-read "default: ") "," \n
  -2 ")," \n)

(define-skeleton drupal6-hook-init
  "Insert hook_init()"
  nil
  "/**\n"
  " * Implementation of hook_init().\n"
  " */" \n
  "function " (drupal6-module-name) "_init() {" \n
  _ \n
  -2 "}" \n)

(define-skeleton drupal6-hook-taxonomy
  "Insert hook_taxonomy()"
  nil
    "/**\n"
  " * Implementation of hook_taxonomy().\n"
  " */" \n
  "function " (drupal6-module-name) "_taxonomy($op, $type, $array = NULL) {" \n
  _ \n
  -2 "}" \n)

(define-skeleton drupal6-comment-block
  "Comment block"
  "Block name: "
  comment-start "{{{ " str comment-end \n
  _ \n
  comment-start "}}} " str comment-end \n)

(defun drupal6 ()
  "Drupal6 init"
  (or (assoc "\\.info$" auto-insert-alist) (define-auto-insert "\\.info$" 'drupal6-info))
  (define-abbrev php-mode-abbrev-table "d6hfa" "" 'drupal6-hook-form-alter)
  (define-abbrev php-mode-abbrev-table "d6hm" "" 'drupal6-hook-menu)
  (define-abbrev php-mode-abbrev-table "d6hb" "" 'drupal6-hook-block)
  (define-abbrev php-mode-abbrev-table "d6fi" "" 'drupal6-form-item)
  (define-abbrev php-mode-abbrev-table "d6f" "" 'drupal6-form)
  (define-abbrev php-mode-abbrev-table "d6is" "" 'drupal6-install-schema)
  (define-abbrev php-mode-abbrev-table "d6ist" "" 'drupal6-install-schema-table)
  (define-abbrev php-mode-abbrev-table "d6istf" "" 'drupal6-install-schema-table-field)
  (define-abbrev php-mode-abbrev-table "d6hi" "" 'drupal6-hook-init)
  (define-abbrev php-mode-abbrev-table "d6ht" "" 'drupal6-hook-taxonomy)
  (global-set-key [?\C-c ?s] 'drupal6-comment-block))

(provide 'drupal6)
;;; drupal6.el ends here
